package com.meituan.controller;

import com.github.pagehelper.PageInfo;
import com.meituan.model.GoodsClassifyEntity;
import com.meituan.model.GoodsEntity;
import com.meituan.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @PostMapping("addGoods")
    public Map addGoods(@RequestBody GoodsEntity goods){
        Map map = new HashMap();
        goods.setRegisterTime(new Date());
        if(goodsService.save(goods)==1){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @GetMapping("getGoodsPage")
    public Map getGoodsList(String shopId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize){
        Map map = new HashMap();
        PageInfo<GoodsEntity> pageInfo = goodsService.findPageByShopIdWithClassify(shopId,pageNum,pageSize);
        if(pageInfo.getList().size()!=0){
            map.put("err_code",0);
            map.put("result",pageInfo);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @GetMapping("delGoods")
    public Map delGoods(String goodsId){
        Map map = new HashMap();
        if(goodsService.delGoods(goodsId)==1){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @PostMapping("updGoods")
    public Map updGoods(@RequestBody GoodsEntity goods){
        Map map = new HashMap();
        if(goodsService.editGoods(goods)==1){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @GetMapping("getGoodsByClassifyId")
    public Map getGoodsByClassifyId(String classifyId){
        Map map = new HashMap();
        List<GoodsEntity> list = goodsService.findByClassifyId(classifyId);
        if(list.size()!=0){
            map.put("err_code",0);
            map.put("result",list);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @GetMapping("getGoodsByShopId")
    public Map getGoodsByShopId(String shopId){
        Map map = new HashMap();
        List<GoodsEntity> list = goodsService.findByShopId(shopId);
        if(list.size()!=0){
            map.put("err_code",0);
            map.put("result",list);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @PostMapping("upload")
    public Map upload(MultipartFile file,String goodsId){
        Map map = new HashMap();
        String fileName = file.getOriginalFilename();
        String fileSuffix =fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdf.format(new Date());
        String finalName = format+'.'+fileSuffix;
        String imgPath = "/static/goodsImg?filename="+finalName;
        goodsService.updateGoodsImg(imgPath,goodsId);
        String diskPath = "G:\\meituanUpload\\goodsImg";;
        File localFile = new File(diskPath,finalName);
        if(!localFile.getParentFile().exists()){
            localFile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(localFile);
            map.put("err_code",0);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("err_code",1);
        }
        return map;
    }
}
